package com.foryou.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foryou.wechat.common.Constants;
import com.foryou.wechat.entity.PlaceTypeEntity;
import com.foryou.wechat.mapper.PlaceTypeMapper;
import com.foryou.wechat.service.PlaceTypeService;
import com.foryou.wechat.utils.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Created by Lee.
 */
@Service
public class PlaceTypeServiceImpl implements PlaceTypeService {

    @Autowired
    private PlaceTypeMapper placeTypeMapper;

    @Override
    public Response addPlaceType(String name) {
        if (StringUtils.isBlank(name)) {
            return Response.failure("请输入场地类别信息！");
        }
        PlaceTypeEntity selectOne = placeTypeMapper.selectOne(
                new QueryWrapper<PlaceTypeEntity>().eq("name", name)
        );
        if (!ObjectUtils.isEmpty(selectOne)) {
            return Response.failure("请场地类别信息已存在！请重新输入");
        }
        placeTypeMapper.insert(new PlaceTypeEntity(name));
        return Response.success();
    }

    @Override
    public Response updatePlaceType(Long placeTypeId, String name) {
        PlaceTypeEntity placeTypeEntity = placeTypeMapper.selectById(placeTypeId);
        if (null != placeTypeEntity && placeTypeEntity.getId().equals(placeTypeId)) {
            throw new RuntimeException(Constants.DATA_EXIST);
        }
        placeTypeMapper.updateById(new PlaceTypeEntity(placeTypeId, name));
        return Response.success();
    }

    @Override
    public Response enabledPlaceType(Long placeTypeId) {
        PlaceTypeEntity placeTypeEntity = placeTypeMapper.selectById(placeTypeId);
        if (null == placeTypeEntity) {
            throw new RuntimeException(Constants.DATA_EXIST);
        }
        int status = placeTypeEntity.getStatus() == 1?0:1;
        placeTypeMapper.updateById(new PlaceTypeEntity(placeTypeId, status));
        return Response.success();
    }

    @Override
    public Response deletePlaceType(Long placeTypeId) {
        placeTypeMapper.deleteById(placeTypeId);
        return Response.success();
    }

    @Override
    public Response getPlaceTypeInfo(Long placeTypeId) {
        PlaceTypeEntity placeTypeEntity = placeTypeMapper.selectById(placeTypeId);
        return Response.success(placeTypeEntity);
    }

    @Override
    public Response getPlaceTypeList() {
        List<PlaceTypeEntity> list = placeTypeMapper.selectList(
                new QueryWrapper<PlaceTypeEntity>().eq("status", 1));
        return Response.success(list);
    }
}
